from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 指定chromedriver路径（137版本）
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

# 创建WebDriver实例
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

# 封装添加服务函数
def add_service(service_data):
    browser.find_element(By.ID, "mainmenutd_products").click()
    browser.find_element(By.CSS_SELECTOR, '.menu_contenu_product_card>a[title="新建服务"]').click()

    # 产品编号
    browser.find_element(By.ID, "ref").send_keys(service_data["ref"])
    # 标签
    browser.find_element(By.ID, "label").send_keys(service_data["label"])
    # 状态（销售）
    browser.find_element(By.CSS_SELECTOR, f"select#statut>option:nth-child({service_data['statut']})").click()
    # 状态（采购）
    browser.find_element(By.CSS_SELECTOR, f"select#statut_buy>option:nth-child({service_data['statut_buy']})").click()
    # 使用批号/序列号
    browser.find_element(By.CSS_SELECTOR, f"select#status_batch>option:nth-child({service_data['status_batch']})").click()
    # 销售日期or保质期
    browser.find_element(By.CSS_SELECTOR, f"select#sell_or_eat_by_mandatory>option:nth-child({service_data['sell_or_eat_by_mandatory']})").click()
    # 描述
    browser.find_element(By.ID, "desc").send_keys(service_data["desc"])
    # 公开网址
    browser.find_element(By.NAME, "url").send_keys(service_data["url"])
    # 默认工作站（2，3，4）
    browser.find_element(By.CSS_SELECTOR, f"select#fk_default_workstation>option:nth-child({service_data['fk_default_workstation']})").click()
    # 为期
    browser.find_element(By.NAME, "duration_value").send_keys(str(service_data["duration_value"]))
    browser.find_element(By.CSS_SELECTOR, f"select#duration_unit>option:nth-child({service_data['duration_unit']})").click()
    # 备注
    browser.find_element(By.ID, "note_private").send_keys(service_data["note_private"])
    # 售价
    browser.find_element(By.NAME, "price").send_keys(str(service_data["price"]))
    # 是否含税金
    browser.find_element(By.CSS_SELECTOR, f"select#select_price_base_type>option:nth-child({service_data['select_price_base_type']})").click()
    # 最低售价
    browser.find_element(By.NAME, "price_min").send_keys(str(service_data["price_min"]))
    # 科目代码（销售）
    browser.find_element(By.NAME, "accountancy_code_sell").send_keys(service_data["accountancy_code_sell"])
    # 科目代码（出口销售）
    browser.find_element(By.NAME, "accountancy_code_sell_export").send_keys(service_data["accountancy_code_sell_export"])
    # 科目代码（采购）
    browser.find_element(By.NAME, "accountancy_code_buy").send_keys(service_data["accountancy_code_buy"])
    # 科目代码（进口采购）
    browser.find_element(By.NAME, "accountancy_code_buy_export").send_keys(service_data["accountancy_code_buy_export"])
    # 创建
    browser.find_element(By.NAME, "add").click()

if __name__ == '__main__':
    try:
        login()
        sleep(1)  # 等待页面加载

        # 服务添加功能测试数据
        # 测试结果：正向全部通过，反向测试第一个提示添加失败，不能空标签，其他全是添加成功
        test_data = [
            # --------------------- 正向测试用例 ---------------------
            {
                "case_desc": "添加标准服务",
                "ref": "SERVICE-001",
                "label": "基础维护服务",
                "statut": 2,  # 可销售状态
                "statut_buy": 2,  # 可采购状态
                "status_batch": 2,  # 使用批号
                "sell_or_eat_by_mandatory": 2,  # 非强制
                "desc": "基础设备维护服务，包含年度检修",
                "url": "https://www.example.com/service-001",
                "fk_default_workstation": 2,  # 第二个工作站
                "duration_value": 30,
                "duration_unit": 2,  # 天
                "note_private": "需提前3天预约",
                "price": 599.0,
                "select_price_base_type": 1,  # 不含税
                "price_min": 499.0,
                "accountancy_code_sell": "600101",
                "accountancy_code_sell_export": "600201",
                "accountancy_code_buy": "140301",
                "accountancy_code_buy_export": "140302",
                "expected_result": "服务添加成功，显示成功提示"
            },
            {
                "case_desc": "添加长期服务",
                "ref": "SERVICE-002",
                "label": "年度VIP服务",
                "statut": 2,
                "statut_buy": 2,
                "status_batch": 2,
                "sell_or_eat_by_mandatory": 2,
                "desc": "年度VIP会员服务，包含12次上门维护",
                "url": "https://www.example.com/service-002",
                "fk_default_workstation": 3,  # 第三个工作站
                "duration_value": 365,
                "duration_unit": 2,  # 天
                "note_private": "VIP客户专享服务",
                "price": 5999.0,
                "select_price_base_type": 2,  # 含税
                "price_min": 5599.0,
                "accountancy_code_sell": "600102",
                "accountancy_code_sell_export": "600202",
                "accountancy_code_buy": "140302",
                "accountancy_code_buy_export": "140303",
                "expected_result": "服务添加成功，显示成功提示"
            },
            {
                "case_desc": "添加短期服务",
                "ref": "SERVICE-003",
                "label": "紧急维修服务",
                "statut": 2,
                "statut_buy": 2,
                "status_batch": 2,
                "sell_or_eat_by_mandatory": 2,
                "desc": "24小时紧急维修服务，1小时响应",
                "url": "https://www.example.com/service-003",
                "fk_default_workstation": 2,
                "duration_value": 1,
                "duration_unit": 3,  # 小时
                "note_private": "紧急服务需额外收费",
                "price": 299.0,
                "select_price_base_type": 1,
                "price_min": 259.0,
                "accountancy_code_sell": "600103",
                "accountancy_code_sell_export": "600203",
                "accountancy_code_buy": "140303",
                "accountancy_code_buy_export": "140304",
                "expected_result": "服务添加成功，显示成功提示"
            },

            # --------------------- 反向测试用例 ---------------------
            {
                "case_desc": "空编号测试",
                "ref": "",  # 空编号
                "label": "测试服务",
                "statut": 2,
                "statut_buy": 2,
                "status_batch": 2,
                "sell_or_eat_by_mandatory": 2,
                "desc": "测试服务描述",
                "url": "https://www.example.com/test",
                "fk_default_workstation": 2,
                "duration_value": 1,
                "duration_unit": 3,
                "note_private": "测试备注",
                "price": 99.0,
                "select_price_base_type": 1,
                "price_min": 89.0,
                "accountancy_code_sell": "600101",
                "accountancy_code_sell_export": "600201",
                "accountancy_code_buy": "140301",
                "accountancy_code_buy_export": "140302",
                "expected_result": "提示'产品编号不能为空'，服务未创建"
            },
            {
                "case_desc": "负价格测试",
                "ref": "SERVICE-004",
                "label": "负价格测试服务",
                "statut": 2,
                "statut_buy": 2,
                "status_batch": 2,
                "sell_or_eat_by_mandatory": 2,
                "desc": "负价格测试",
                "url": "https://www.example.com/negative-price",
                "fk_default_workstation": 2,
                "duration_value": 1,
                "duration_unit": 3,
                "note_private": "负价格测试备注",
                "price": -99.0,  # 负价格
                "select_price_base_type": 1,
                "price_min": -89.0,
                "accountancy_code_sell": "600101",
                "accountancy_code_sell_export": "600201",
                "accountancy_code_buy": "140301",
                "accountancy_code_buy_export": "140302",
                "expected_result": "提示'价格不能为负数'，服务未创建"
            },
            {
                "case_desc": "超长标签测试",
                "ref": "SERVICE-005",
                "label": "a" * 256,  # 256个字符的超长标签（假设系统限制255字符）
                "statut": 2,
                "statut_buy": 2,
                "status_batch": 2,
                "sell_or_eat_by_mandatory": 2,
                "desc": "超长标签测试描述",
                "url": "https://www.example.com/long-label",
                "fk_default_workstation": 2,
                "duration_value": 1,
                "duration_unit": 3,
                "note_private": "超长标签测试备注",
                "price": 99.0,
                "select_price_base_type": 1,
                "price_min": 89.0,
                "accountancy_code_sell": "600101",
                "accountancy_code_sell_export": "600201",
                "accountancy_code_buy": "140301",
                "accountancy_code_buy_export": "140302",
                "expected_result": "提示'标签长度不能超过255字符'，服务未创建"
            }
        ]
        for data in test_data:
            add_service(data)
            sleep(1)  # 等待操作完成
    except Exception as e:
        print("发生错误：", e)
    finally:
        sleep(1)
        browser.quit()